//
//  GQHMeView.m
//  Xanthium
//
//  Created by GuanQinghao on 24/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHMeView.h"
#import "GQHHeader.h"
#import "QHRatingView.h"

@interface GQHMeView () <UITableViewDelegate,UITableViewDataSource> {
    
    NSArray *iconArray;
    NSArray *titleArray;
}

@end

@implementation GQHMeView
#pragma mark --Lifecycle
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    iconArray = @[@"me_transaction",@"me_order",@"me_order",@"me_order",@"me_evaluation",@"me_truck",@"me_evaluation",@"me_evaluation",@"me_evaluation",@"me_truck",@"me_truck"];
    titleArray = @[@"我的转账",@"我的发布",@"我的运单",@"我的运单",@"我的报价",@"我的车辆",@"我的货源",@"我的评价",@"我关注的货主",@"我关注的车辆",@"换车"];
//    titleArray = @[@"我的转账(司机)(货主)",@"我的运单(司机)",@"我的运单(货主)",@"我的报价(司机)",@"我的车辆(司机)",@"我的货源(货主)",@"我的评价(司机)(货主)",@"我关注的货主(司机)",@"我关注的车辆(货主)"];
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _qh_tableView.separatorColor = kLightGrayColor;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).with.offset(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.bottom.mas_equalTo(self).with.inset(self.qh_tabBarHeight + self.qh_homeIndicatorHeight);
        make.left.and.right.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    /// 根据角色隐藏
    if ((3 == indexPath.row || 6 == indexPath.row || 9 == indexPath.row) && [USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        return CGFLOAT_MIN;
    }
    
    if ((1 == indexPath.row || 2 == indexPath.row || 4 == indexPath.row || 5 == indexPath.row || 8 == indexPath.row || indexPath.row == 10) && ![USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        return CGFLOAT_MIN;
    }
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    /// 根据角色隐藏
    if ((3 == indexPath.row || 6 == indexPath.row || 9 == indexPath.row) && [USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        cell.hidden = YES;
    }
    
    if ((1 == indexPath.row || 2 == indexPath.row || 4 == indexPath.row || 5 == indexPath.row || 8 == indexPath.row || indexPath.row == 10) && ![USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        cell.hidden = YES;
    }
    cell.imageView.image = [UIImage imageNamed:iconArray[indexPath.row]];
    cell.textLabel.font = kDefaultFont;
    cell.textLabel.text = titleArray[indexPath.row];
    cell.textLabel.textColor = kDarkBlackColor;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.5 * self.qh_width;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = kLightGrayColor;
    
    UIView *userInfoView = [[UIView alloc] init];
    userInfoView.layer.contents = (id)[UIImage imageNamed:@"me_background_image"].CGImage;
    [headerView addSubview:userInfoView];
    [userInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.top.and.right.mas_equalTo(headerView);
        make.bottom.mas_equalTo(headerView).with.inset(55);
    }];
    
    /// 头像
    UIImageView *avatarImageView = [[UIImageView alloc] init];
    avatarImageView.layer.cornerRadius = 5.0f;
    avatarImageView.layer.masksToBounds = YES;
    NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:USER_GET_AVATAR];
    NSURL *URL = [NSURL URLWithString:URLString];
    [avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
    [userInfoView addSubview:avatarImageView];
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(userInfoView).with.inset(GQH_MARGIN_SINGLE);
        make.top.and.bottom.mas_equalTo(userInfoView).with.inset(GQH_MARGIN_SINGLE);
        make.width.mas_equalTo(avatarImageView.mas_height).with.multipliedBy(1.0);
    }];
    
    UILabel *userNameLabel = [[UILabel alloc] init];
    userNameLabel.backgroundColor = kClearColor;
    userNameLabel.font = kBiggerFont;
    userNameLabel.text = USER_GET_NAME;
    userNameLabel.textColor = kWhiteColor;
    userNameLabel.textAlignment = NSTextAlignmentLeft;
    [userInfoView addSubview:userNameLabel];
    [userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(avatarImageView.mas_right).with.offset(10);
        make.top.mas_equalTo(avatarImageView);
        make.right.mas_equalTo(userInfoView).with.inset(GQH_MARGIN_SINGLE);
        make.height.mas_equalTo(avatarImageView).with.multipliedBy(0.4);
    }];
    
    UIView *starView = [[UIView alloc] init];
    starView.backgroundColor = kClearColor;
    [userInfoView addSubview:starView];
    [starView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(userNameLabel.mas_bottom);
        make.left.mas_equalTo(userNameLabel);
        make.height.mas_equalTo(avatarImageView).with.multipliedBy(0.3);
        make.width.mas_equalTo(starView.mas_height).with.multipliedBy(5);
    }];
    
    CGRect frame = CGRectMake(0, 0, 1.5 * (0.5 * self.qh_width - 95), 0.3 * (0.5 * self.qh_width - 95));
    QHRatingView *ratingView = [[QHRatingView alloc] initWithFrame:frame];
    ratingView.backgroundColor = kClearColor;
    CGFloat score = [USER_GET_SCORE floatValue];
    [ratingView showStarWithValue:score];
    [starView addSubview:ratingView];
    
    UILabel *scoreLabel = [[UILabel alloc] init];
    scoreLabel.backgroundColor = kClearColor;
    scoreLabel.font = kSmallerFont;
    scoreLabel.text = [NSString stringWithFormat:@"%1.2f 分",score];
    scoreLabel.textColor = kWhiteColor;
    scoreLabel.textAlignment = NSTextAlignmentLeft;
    [userInfoView addSubview:scoreLabel];
    [scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(starView.mas_right);
        make.top.and.bottom.mas_equalTo(starView);
        make.right.mas_equalTo(userInfoView).with.inset(GQH_MARGIN_SINGLE);
    }];
    
    UILabel *membershipLabel = [[UILabel alloc] init];
    membershipLabel.backgroundColor = kClearColor;
    membershipLabel.font = kSmallerFont;
    membershipLabel.text = @"智运金牌会员";
    membershipLabel.textColor = kAppYellowColor;
    membershipLabel.textAlignment = NSTextAlignmentLeft;
    [userInfoView addSubview:membershipLabel];
    [membershipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(starView.mas_bottom);
        make.left.mas_equalTo(userNameLabel);
        make.right.mas_equalTo(userNameLabel);
        make.height.mas_equalTo(avatarImageView).with.multipliedBy(0.4);
    }];
    
    UIButton *membershipButton = [UIButton buttonWithType:UIButtonTypeCustom];
    membershipButton.backgroundColor = kWhiteColor;
    membershipButton.titleLabel.font = kDefaultFont;
    [membershipButton setTitle:@"我的会员" forState:UIControlStateNormal];
    [membershipButton setTitleColor:kDarkBlackColor forState:UIControlStateNormal];
    [membershipButton addTarget:self action:@selector(checkMembership:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:membershipButton];
    [membershipButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(userInfoView.mas_bottom);
        make.left.mas_equalTo(headerView);
        make.width.mas_equalTo(headerView).with.multipliedBy(0.5);
        make.height.mas_equalTo(50);
    }];
    
    UIButton *authenticationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    authenticationButton.backgroundColor = kWhiteColor;
    authenticationButton.titleLabel.font = kDefaultFont;
    if ([USER_GET_STATUS isEqualToString:USER_STATUS_S0]) {
        
        [authenticationButton setTitle:@"已认证" forState:UIControlStateNormal];
    } else if ([USER_GET_STATUS isEqualToString:USER_STATUS_S1]) {
        
        [authenticationButton setTitle:@"认证中" forState:UIControlStateNormal];
    } else if ([USER_GET_STATUS isEqualToString:USER_STATUS_S2]) {
        
        [authenticationButton setTitle:@"未认证" forState:UIControlStateNormal];
    } else if ([USER_GET_STATUS isEqualToString:USER_STATUS_S3]) {
        
        [authenticationButton setTitle:@"认证失败" forState:UIControlStateNormal];
    }else if ([USER_GET_STATUS isEqualToString:USER_STATUS_S4]) {
        
        [authenticationButton setTitle:@"待完善" forState:UIControlStateNormal];
    } else {
        
        [authenticationButton setTitle:@"未知状态" forState:UIControlStateNormal];
    }
    [authenticationButton setTitleColor:kDarkBlackColor forState:UIControlStateNormal];
    [authenticationButton addTarget:self action:@selector(submitOrCheckAuthorizedInformation:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:authenticationButton];
    [authenticationButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(membershipButton.mas_right);
        make.top.mas_equalTo(membershipButton);
        make.width.mas_equalTo(membershipButton);
        make.height.mas_equalTo(membershipButton);
    }];
    
    UIView *separatorView = [[UIView alloc] init];
    separatorView.backgroundColor = kLightGrayColor;
    separatorView.layer.cornerRadius = 1.0;
    separatorView.layer.masksToBounds = YES;
    [headerView addSubview:separatorView];
    [separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(2, 30));
        make.centerY.mas_equalTo(membershipButton);
        make.centerX.mas_equalTo(headerView);
    }];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

/// 查看认证信息或去认证
- (IBAction)submitOrCheckAuthorizedInformation:(UIButton *)sender {
    
    if ([USER_GET_STATUS isEqualToString:USER_STATUS_S0]) {
        DLog(@"已认证");
        
        if ([_qh_delegate respondsToSelector:@selector(qh_checkAuthorizedInformation)]) {
            
            [_qh_delegate qh_checkAuthorizedInformation];
        }
    } else if ([USER_GET_STATUS isEqualToString:USER_STATUS_S1]) {
        DLog(@"待认证");
        
        if ([_qh_delegate respondsToSelector:@selector(qh_checkAuthorizedInformation)]) {
            
            [_qh_delegate qh_checkAuthorizedInformation];
        }
    } else if ([USER_GET_STATUS isEqualToString:USER_STATUS_S2]) {
        DLog(@"未认证");
        
        if ([_qh_delegate respondsToSelector:@selector(qh_submitAuthorizedInformation)]) {
            
            [_qh_delegate qh_submitAuthorizedInformation];
        }
    } else if ([USER_GET_STATUS isEqualToString:USER_STATUS_S3]) {
        DLog(@"认证失败");
        
        if ([_qh_delegate respondsToSelector:@selector(qh_checkAuthorizedInformation)]) {
            
            [_qh_delegate qh_checkAuthorizedInformation];
        }
    }else if ([USER_GET_STATUS isEqualToString:USER_STATUS_S4]) {
        DLog(@"待完善");
        
        if ([_qh_delegate respondsToSelector:@selector(qh_submitAuthorizedInformation)]) {
            
            [_qh_delegate qh_submitAuthorizedInformation];
        }
    } else {
        DLog(@"未知状态");
        
    }
}

/// 查看会员信息
- (IBAction)checkMembership:(UIButton *)sender {
    
    if ([_qh_delegate respondsToSelector:@selector(qh_checkMembership)]) {
        
        [_qh_delegate qh_checkMembership];
    }
}

@end
